package cn.iocoder.an.module.business.controller.admin.reportviewnew;

import cn.iocoder.an.module.business.dal.dataobject.reportviewnew.ReportConfigDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.reportviewnew.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.reportviewnew.ReportViewNewDO;
import cn.iocoder.an.module.business.service.reportviewnew.ReportViewNewService;

@Tag(name = "管理后台 - 消息推送配置")
@RestController
@RequestMapping("/business/report-view-new")
@Validated
public class ReportViewNewController {

    @Resource
    private ReportViewNewService reportViewNewService;

    @PostMapping("/create")
    @Operation(summary = "创建消息推送配置")
    @PreAuthorize("@ss.hasPermission('business:report-view-new:create')")
    public CommonResult<Long> createReportViewNew(@Valid @RequestBody ReportViewNewSaveReqVO createReqVO) {
        return success(reportViewNewService.createReportViewNew(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新消息推送配置")
    @PreAuthorize("@ss.hasPermission('business:report-view-new:update')")
    public CommonResult<Boolean> updateReportViewNew(@Valid @RequestBody ReportViewNewSaveReqVO updateReqVO) {
        reportViewNewService.updateReportViewNew(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除消息推送配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:report-view-new:delete')")
    public CommonResult<Boolean> deleteReportViewNew(@RequestParam("id") Long id) {
        reportViewNewService.deleteReportViewNew(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除消息推送配置")
                @PreAuthorize("@ss.hasPermission('business:report-view-new:delete')")
    public CommonResult<Boolean> deleteReportViewNewList(@RequestParam("ids") List<Long> ids) {
        reportViewNewService.deleteReportViewNewListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得消息推送配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:report-view-new:query')")
    public CommonResult<ReportViewNewRespVO> getReportViewNew(@RequestParam("id") Long id) {
        ReportViewNewDO reportViewNew = reportViewNewService.getReportViewNew(id);
        return success(BeanUtils.toBean(reportViewNew, ReportViewNewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得消息推送配置分页")
    @PreAuthorize("@ss.hasPermission('business:report-view-new:query')")
    public CommonResult<PageResult<ReportViewNewRespVO>> getReportViewNewPage(@Valid ReportViewNewPageReqVO pageReqVO) {
        PageResult<ReportViewNewDO> pageResult = reportViewNewService.getReportViewNewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportViewNewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出消息推送配置 Excel")
    @PreAuthorize("@ss.hasPermission('business:report-view-new:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportViewNewExcel(@Valid ReportViewNewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportViewNewDO> list = reportViewNewService.getReportViewNewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "消息推送配置.xls", "数据", ReportViewNewRespVO.class,
                        BeanUtils.toBean(list, ReportViewNewRespVO.class));
    }

    // ==================== 子表（报表消息推送配置） ====================

    @GetMapping("/report-config/list-by-report-id")
    @Operation(summary = "获得报表消息推送配置列表")
    @Parameter(name = "reportId", description = "父表id")
    @PreAuthorize("@ss.hasPermission('business:report-view-new:query')")
    public CommonResult<List<ReportConfigDO>> getReportConfigListByReportId(@RequestParam("reportId") Long reportId) {
        return success(reportViewNewService.getReportConfigListByReportId(reportId));
    }

}